//
//  SWUIToolKit.swift
//  ReservationClient
//
//  Created by Stan Wu on 15/3/28.
//  Copyright (c) 2015 Stan Wu. All rights reserved.
//

import Foundation
import UIKit
import CoreImage

extension UIAlertView{
    class func show(title:String?,message:String?,cancelButton:String?){
        var cancel = cancelButton;
        if  cancel==nil {
            cancel = "确定"
        }
        UIAlertView.show(title, message: message, cancelButton: cancel, delegate: nil)
    }
    
    class func show(title:String?,message:String?,cancelButton:String?,delegate:UIAlertView?){
        let alert:UIAlertView = UIAlertView(title: title, message: message, delegate: delegate, cancelButtonTitle: cancelButton)
        alert.show()
        
    }
}

extension UIViewController{
    enum NaviButtonPosition{
        case Left,Right
    }
    
    func addNavButton(title: String,action: Selector,target: AnyObject?,position:NaviButtonPosition){
        let item = UIBarButtonItem(title: title, style: UIBarButtonItemStyle.Plain, target: target, action: action)
        switch position{
        case .Left:
            self.navigationItem.leftBarButtonItem = item
        case .Right:
            self.navigationItem.rightBarButtonItem = item
        }
    }
    
    func addBGColor(){
        self.view.backgroundColor = UIColor.whiteColor()
    }
    
    func setNavTitle(title:String){
        self.navigationItem.title = title
        
    }
    
    func addNavBack(){
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(UIViewController.navBack))
    }
    
    func setNavBGImage(){
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "title_bar"), forBarMetrics: UIBarMetrics.Default)
    }
    
    func navBack(){
        self.navigationController?.popViewControllerAnimated(true)
    }
}

extension UILabel{
    class func create(frame:CGRect,font:UIFont,textColor:UIColor) -> UILabel{
        let lbl = UILabel(frame: frame)
        lbl.font = font
        lbl.textColor = textColor
        lbl.backgroundColor = UIColor.clearColor()
        lbl.userInteractionEnabled = false
        
        return lbl
    }
}

extension UIImage{
    class func colorImage(color:UIColor,size:CGSize) -> UIImage{
        var cicolor = CIColor(CGColor: color.CGColor)

        cicolor = CIColor(red: cicolor.red, green: cicolor.green, blue: cicolor.blue, alpha: cicolor.alpha)
        

        typealias MYImage = CoreImage.CIImage
        let ciimg:MYImage = MYImage(color:cicolor)
        if #available(iOS 9.0, *) {
            
            //高于 iOS 9.0
            let ctx = CIContext(options: nil);
            let imgref = ctx.createCGImage(ciimg, fromRect: CGRectMake(0, 0, size.width, size.height))
            let img = UIImage(CGImage: imgref)
            return img;
        } else {
            
            //低于 iOS 9.0
            
            let ctx = CIContext();
            let imgref = ctx.createCGImage(ciimg, fromRect: CGRectMake(0, 0, size.width, size.height))
//            if (imgref != nil) {
                let img = UIImage(CGImage: imgref)
                return img;
//
//            }else{
//                
//                let img = UIImage(CIImage: ciimg)
//                return img;
//
//            }
        }
        

    
    }
}

protocol SWImageViewDelegate{
    func swImageViewLoadFinished(imgv:SWImageView)
}

class SWImageView: UIImageView{
    var delegate:SWImageViewDelegate?
    
    func loadURL(str: String?){
        if let url = str{
            var ary = url.componentsSeparatedByString("/")
            let fileName = NSMutableString()
            
        

            for i in 0 ..< ary.count{
                fileName.appendString(ary[i])
            }
            
            let path = (fileName as String).imageCachePath()

            if NSFileManager.defaultManager().fileExistsAtPath(path){
                self.image = UIImage(contentsOfFile: path)
                
                self.delegate?.swImageViewLoadFinished(self)
            }else{
                NSThread.detachNewThreadSelector(#selector(SWImageView.loadImage(_:)), toTarget: self, withObject: str)
            }
        }
    }
    
    func loadImage(str: String){
        autoreleasepool { () -> () in
            let url = NSURL(string: str)
            let data = NSData(contentsOfURL: url!)
            
            if let d = data{
                let img = UIImage(data: d)
                
                
                if img != nil{
                    SWUtils.RunOnMainThread({ () -> Void in
                        self.image = img
                        
                        var ary = str.componentsSeparatedByString("/")
                        let fileName = NSMutableString()
                        
                        
                        
                        for i in 0 ..< ary.count{
                            fileName.appendString(ary[i])
                        }
                        
                        let path = (fileName as String).imageCachePath()
                        
                        data?.writeToFile(path, atomically: false)
                        
                        self.delegate?.swImageViewLoadFinished(self)
                    })
                }
            }
            
        }
    }
}


